/*
libhd44780fw C++ wrapper.
Copyright (C) 2011  Philippe Proulx (eeppeliteloop@gmail.com)

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <avr/io.h>

#include "hd44780fw_cppwrapper.hpp"
#include "hd44780fw.h"
#include "hd44780_low.h"

hd44780::hd44780(const struct hd44780fw_conf* conf) {
	this->fw_conf = *conf;
	this->low_conf = *(conf->low_conf);
	this->fw_conf.low_conf = &this->low_conf;
	this->init();
}

void hd44780::init(void) {
	::hd44780fw_init(&this->fw_conf);
}

void hd44780::put(char ch) {
	::hd44780fw_put(&this->fw_conf, ch);
}

void hd44780::write(const char* msg, uint8_t index, uint8_t cb) {
	::hd44780fw_write(&this->fw_conf, msg, index, cb);
}

void hd44780::clear(void) {
	::hd44780fw_clear(&this->fw_conf);
}

void hd44780::en_blink(bool state) {
	::hd44780fw_en_blink(&this->fw_conf,
		(state ? HD44780_L_DISP_B_ON : HD44780_L_DISP_B_OFF));
}

void hd44780::en_cursor(bool state) {
	::hd44780fw_en_cursor(&this->fw_conf,
		(state ? HD44780_L_DISP_C_ON : HD44780_L_DISP_C_OFF));
}

void hd44780::set_bc_index(uint8_t index) {
	::hd44780fw_set_bc_index(&this->fw_conf, index);
}

void hd44780::build_cc(uint8_t index, const uint8_t* rows) {
	::hd44780fw_build_cc(&this->fw_conf, index, rows);
}

hd44780& hd44780::operator<<(const char* msg) {
	::hd44780fw_cat_string(&this->fw_conf, msg);
	
	return *this;
}

hd44780& hd44780::operator<<(uint16_t u) {
	::hd44780fw_cat_uint16(&this->fw_conf, u);
	
	return *this;
}

hd44780& hd44780::operator<<(uint8_t u) {
	::hd44780fw_cat_uint8(&this->fw_conf, u);
	
	return *this;
}

hd44780& hd44780::operator<<(int16_t i) {
	::hd44780fw_cat_int16(&this->fw_conf, i);
	
	return *this;
}

hd44780& hd44780::operator<<(int8_t i) {
	::hd44780fw_cat_int8(&this->fw_conf, i);
	
	return *this;
}

hd44780& hd44780::operator<<(char ch) {
	::hd44780fw_cat_char(&this->fw_conf, ch);
	
	return *this;
}

const char* hd44780::operator=(const char* msg) {
	::hd44780fw_write(&this->fw_conf, msg, 0, 1);
	
	return msg;
}

void hd44780::operator-=(uint8_t rem) {
	::hd44780fw_rem(&this->fw_conf, rem);
}
